package com.zzyl.nursing.service.impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.nursing.domain.Reservation;
import com.zzyl.nursing.dto.ReservationCountDto;
import com.zzyl.nursing.mapper.ReservationMapper;
import com.zzyl.nursing.service.IReservationService;
import com.zzyl.nursing.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

/**
 * 预约信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-07-31
 */
@Service
public class ReservationServiceImpl extends ServiceImpl<ReservationMapper, Reservation> implements IReservationService
{
    @Autowired
    private ReservationMapper reservationMapper;

    /**
     * 查询预约信息
     * 
     * @param id 预约信息主键
     * @return 预约信息
     */
    @Override
    public Reservation selectReservationById(Long id)
    {
        return getById(id);
    }

    /**
     * 查询预约信息列表
     * 
     * @param
     * @return 预约信息
     */
    @Override
    public PageResult<Reservation> selectReservationList(Integer pageNum, Integer pageSize, Integer status)
    {

        PageHelper.startPage(pageNum, pageSize);

        List<Reservation> reservations = reservationMapper.selectReservationList(status);
        Page<Reservation> page = (Page<Reservation>) reservations;

        return new PageResult<>(page.getTotal(),page.getResult());

    }

    /**
     * 新增预约信息
     * 
     * @param reservation 预约信息
     * @return 结果
     */
    @Override
    public int insertReservation(Reservation reservation)
    {
        reservation.setStatus(0);
        boolean bool = save(reservation);
        return  bool? 1 : 0;
    }

    /**
     * 修改预约信息
     * 
     * @param reservation 预约信息
     * @return 结果
     */
    @Override
    public int updateReservation(Reservation reservation)
    {
        return updateById(reservation) ? 1 : 0;
    }

    /**
     * 批量删除预约信息
     * 
     * @param ids 需要删除的预约信息主键
     * @return 结果
     */
    @Override
    public int deleteReservationByIds(Long[] ids)
    {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除预约信息信息
     * 
     * @param id 预约信息主键
     * @return 结果
     */
    @Override
    public int deleteReservationById(Long id)
    {
        return removeById(id) ? 1 : 0;
    }

    /**
     * 查询预约信息信息
     * @param userId
     * @return
     */
    @Override
    public Integer getCancelledReservationCount(Long userId) {

        long time = System.currentTimeMillis();//获取当前时间
        LocalDateTime ldt = LocalDateTimeUtil.of(time);//获取当前时间对应的LocalDateTime对象
        LocalDateTime startTime = ldt.toLocalDate().atStartOfDay();//获取当前时间对应的LocalDateTime对象
        LocalDateTime endTime = startTime.plusDays(1);

        ReservationCountDto reservationCountDto = ReservationCountDto.builder()
                .userId(userId)
                .ldt(ldt)
                .startTime(startTime)
                .endTime(endTime).build();

        return  reservationMapper.getCancelledReservationCount(reservationCountDto);

    }

    @Override
    public void cancle(Long id) {
        //更新状态

//这个更新方式不会触发MyMetaObjectHandler自动填充功能，因为它直接使用了LambdaUpdateWrapper设置字段值，绕过了MyBatis Plus的实体对象自动填充机制。
//        this.update(Wrappers.<Reservation>lambdaUpdate()
//                .eq(Reservation::getId,id)
//                .set(Reservation::getStatus,2));


        Reservation reservation = new Reservation();
        reservation.setId(id);
        reservation.setStatus(2);
        updateById(reservation);

    }

    /**
     * 跟新合同状态
     */
    @Override
    public void updateReservationStatus() {

        //把要更新的取出来
        List<Reservation> reservationList = this.list(Wrappers.<Reservation>lambdaQuery()
                .eq(Reservation::getStatus, 0)
                .le(Reservation::getTime, LocalDateTime.now()) //签合同时间小于当前时间
        );
        //把取出来的数据的状态更新了
        if (CollectionUtils.isEmpty(reservationList)) {
            return ;
        }
        reservationList.forEach(reservation -> {
            reservation.setStatus(3);
        });

        updateBatchById(reservationList);//批量更新


    }
}
